/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   LevelAdministrationServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.industrymanage    </p>
 * 
 * <p>最后修改 : $: 2017年8月17日-下午3:42:03 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.industrymanage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.industrymanage.LevelAdministrationService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * LevelAdministrationServiceImpl
 * 
 * 2017年8月17日 下午3:42:03
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class LevelAdministrationServiceImpl implements LevelAdministrationService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	
	@Override
	public Map<String, Object> getLevelAdministrationMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取发展规划
		List<Attachment> glmbList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "gjgl_ndglmb");
		List<Attachment> jgglList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "gjgl_hyjggl");
		List<Attachment> ndkhList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "gjgl_ndkh");
		
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		List<String> glmbResult = new ArrayList<String>(years.length);
		List<String> jgglResult = new ArrayList<String>(years.length);
		List<String> ndkhResult = new ArrayList<String>(years.length);
		for ( int i = 0; i<years.length; i++){
			//判断每年是否有管理目标文件
			if (glmbList.size() <= 0) {
				glmbResult.add("0");
			} else {
				for (int j = 0; j< glmbList.size(); j++) {
					if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", glmbList.get(j).getFileTime()))) {
						if (glmbResult.size() <= i) {
							glmbResult.add("1");
						}
						if ("0".equals(glmbResult.get(i))) {
							glmbResult.set(i, "1");
						}
						break; 
					} else {
						if (glmbResult.size() <= i) {
							glmbResult.add("0");
						}
					}
				}
			}
			
			//判断每年是否有监管管理文件
			if (jgglList.size() <= 0) {
				jgglResult.add("0");
			} else {
				for (int j = 0; j< jgglList.size(); j++) {
					if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", jgglList.get(j).getFileTime()))) {
						if (jgglResult.size() <= i) {
							jgglResult.add("1");
						}
						if ("0".equals(jgglResult.get(i))) {
							jgglResult.set(i, "1");
						}
						break; 
					} else {
						if (jgglResult.size() <= i) {
							jgglResult.add("0");
						}
					}
				}
			}
			
			//判断每年是否有年度考核文件
			if (ndkhList.size() <= 0) {
				ndkhResult.add("0");
			} else {
				for (int j = 0; j< ndkhList.size(); j++) {
					if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", ndkhList.get(j).getFileTime()))) {
						if (ndkhResult.size() <= i) {
							ndkhResult.add("1");
						}
						if ("0".equals(ndkhResult.get(i))) {
							ndkhResult.set(i, "1");
						}
						break; 
					} else {
						if (ndkhResult.size() <= i) {
							ndkhResult.add("0");
						}
					}
				}
			}
		}
		
		int glmbNum = 0;
		int jgglNum = 0;
		int ndkhNum = 0;
		for (int j = 0; j < glmbResult.size(); j++) {
			glmbNum += glmbResult.get(j).equals("1") ? 1 : 0;
		}
		for (int j = 0; j < jgglResult.size(); j++) {
			jgglNum += jgglResult.get(j).equals("1") ? 1 : 0;
		}
		for (int j = 0; j < ndkhResult.size(); j++) {
			ndkhNum += ndkhResult.get(j).equals("1") ? 1 : 0;
		}
		
		if (glmbNum + jgglNum + ndkhNum >= years.length*3) {
			total = yearFull;
		} else if ((glmbNum + jgglNum) >= years.length*2 || (glmbNum + ndkhNum) >= years.length*2 || (jgglNum + ndkhNum) >= years.length*2) {
			total = yearPart;
		} else if (glmbNum < years.length && jgglNum < years.length && ndkhNum < years.length) {
			total = 0;
		} else {
			total = poitMark;
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		map.put("percent", percentage);
		map.put("glmbResult", glmbResult);
		map.put("jgglResult", jgglResult);
		map.put("ndkhResult", ndkhResult);
		map.put("glmbNum", glmbNum);
		map.put("jgglNum", jgglNum);
		map.put("ndkhNum", ndkhNum);
		map.put("years", years);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
